import 'package:flutter/material.dart';
import './data.dart';

class ListDome extends StatefulWidget {
  const ListDome({super.key});

  @override
  State<ListDome> createState() => _ListDomeState();
}

class _ListDomeState extends State<ListDome> {
  int _current = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('list'),
      ),
      body: Column(
        // mainAxisSize: MainAxisSize.min,
        children: [
          Row(
            children: [
              ElevatedButton(
                onPressed: (){
                  setState(() {
                    _current = 0;
                  });
                },
                child: Text('列表1'),
              ),
              ElevatedButton(
                onPressed: (){
                  setState(() {
                    _current = 1;
                  });
                },
                child: Text('列表2'),
              ),
              ElevatedButton(
                onPressed: (){
                  setState(() {
                    _current = 2;
                  });
                },
                child: Text('横向列表'),
              ),
              ElevatedButton(
                onPressed: (){
                  setState(() {
                    _current = 3;
                  });
                },
                child: Text('动态列表'),
              ),
              ElevatedButton(
                onPressed: (){
                  setState(() {
                    _current = 4;
                  });
                },
                child: Text('动态列表2'),
              ),
              ElevatedButton(
                onPressed: (){
                  setState(() {
                    _current = 5;
                  });
                },
                child: Text('动态列表3'),
              ),
            ],
          ),
          _current == 2 
            ?  List3() 
            : Expanded(
              child: _current == 5 ? List6() : _current == 4 ? List5() : _current == 3 ? List4() : _current == 0 ? List1() : List2(),
            )
          
        ],
      )
    );  
  }
}

class List1 extends StatelessWidget {
  const List1({super.key});

  @override
  Widget build(BuildContext context) {
    return ListView(
        children: <Widget>[
          const ListTile(
            leading: Icon(Icons.payment, color: Colors.green,),
            title: Text('代付款'),
          ),
          const Divider(),
          const ListTile(
            leading: Icon(Icons.favorite,color: Colors.lightGreen,),
            title: Text('我的收藏'),
            trailing: Icon(Icons.chevron_right_sharp),  
          ),
          const Divider(),
          const ListTile(
            leading: Icon(Icons.people,color: Colors.lightGreen,),
            title: Text('在线客服'),
            trailing: Icon(Icons.chevron_right_sharp),  
          ),
          const Divider(),
          ListTile(
            leading: Image.network('https://www.itying.com/images/flutter/1.png'),
            title: const Text('这是标题'),
            subtitle: const Text('这是描述'),
          ),
          const Divider(),
          ListTile(
            leading: Image.network('https://www.itying.com/images/flutter/1.png'),
            title: const Text('这是标题'),
            subtitle: const Text('这是描述'),
            trailing: Image.network('https://www.itying.com/images/flutter/1.png'),
          ),
          const Divider(),
          ListTile(
            title: const Text('这是标题'),
            subtitle: const Text('这是描述'),
            trailing: Image.network('https://www.itying.com/images/flutter/1.png'),
          ),
          const Divider(),
          const ListTile(title: Text('这是列表'),),
          const Divider(),
          const ListTile(title: Text('这是列表'),),
          const Divider(),
          const ListTile(title: Text('这是列表'),),
          const Divider(),
          const ListTile(title: Text('这是列表'),),
          const Divider(),
          const ListTile(title: Text('这是列表'),),
          const Divider(),
          const ListTile(title: Text('这是列表'),),
          const Divider(),
          const ListTile(title: Text('这是列表'),),
          const Divider(),
          const ListTile(title: Text('这是列表'),),
          const Divider(),
        ],
    );
  }
}


class List2 extends StatelessWidget {
  const List2({super.key});

  @override
  Widget build(BuildContext context) {
    return ListView( // 竖排 width 根据父级元素自动，设置无效
      children: [
        Image.network('https://www.itying.com/images/flutter/1.png', height: 100,),
        Container(
          padding: EdgeInsets.all(10),
          height: 44,
          child: Text('这是一个标题', textAlign: TextAlign.center,style: TextStyle(fontSize: 20),),
        ),
        Image.network('https://www.itying.com/images/flutter/1.png', width: 150, height: 100,),
        Container(
          padding: EdgeInsets.all(10),
          height: 44,
          child: Text('这是一个标题', textAlign: TextAlign.center,style: TextStyle(fontSize: 20),),
        ),
        Image.network('https://www.itying.com/images/flutter/1.png', width: 150, height: 100,),
        Container(
          padding: EdgeInsets.all(10),
          height: 44,
          child: Text('这是一个标题', textAlign: TextAlign.center,style: TextStyle(fontSize: 20),),
        ),
        Image.network('https://www.itying.com/images/flutter/1.png', width: 150, height: 100,),
        Container(
          padding: EdgeInsets.all(10),
          height: 44,
          child: Text('这是一个标题', textAlign: TextAlign.center,style: TextStyle(fontSize: 20),),
        ),
      ]
    );
  }
}

// 横向列表
class List3 extends StatelessWidget {
  const List3({super.key});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 140,
      child: ListView(
        shrinkWrap: true,
        scrollDirection: Axis.horizontal,
        padding: EdgeInsets.all(10),      
        children: [
          Container(
            width: 100,
            margin: EdgeInsets.all(10),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.red,)
            ),
            child: Column(
              children: [
                SizedBox(
                  height: 70,
                  child: Image.network('https://www.itying.com/images/flutter/1.png', fit: BoxFit.cover,),
                ),
                Text('长沙市')
              ]
            ),
          ),
          Container(
            width: 100,
            margin: EdgeInsets.all(10),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.red,)
            ),
            child: Column(
              children: [
                SizedBox(
                  height: 70,
                  child: Image.network('https://www.itying.com/images/flutter/1.png', fit: BoxFit.cover,),
                ),
                Text('长沙市')
              ]
            ),
          ),
          Container(
            width: 100,
            margin: EdgeInsets.all(10),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.red,)
            ),
            child: Column(
              children: [
                SizedBox(
                  height: 70,
                  child: Image.network('https://www.itying.com/images/flutter/1.png', fit: BoxFit.cover,),
                ),
                Text('长沙市')
              ]
            ),
          ),
          Container(
            width: 100,
            margin: EdgeInsets.all(10),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.red,)
            ),
            child: Column(
              children: [
                SizedBox(
                  height: 70,
                  child: Image.network('https://www.itying.com/images/flutter/1.png', fit: BoxFit.cover,),
                ),
                Text('长沙市')
              ]
            ),
          ),
          Container(
            width: 100,
            margin: EdgeInsets.all(10),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.red,)
            ),
            child: Column(
              children: [
                SizedBox(
                  height: 70,
                  child: Image.network('https://www.itying.com/images/flutter/1.png', fit: BoxFit.cover,),
                ),
                Text('长沙市')
              ]
            ),
          ),
          Container(
            width: 100,
            margin: EdgeInsets.all(10),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.red,)
            ),
            child: Column(
              children: [
                SizedBox(
                  height: 70,
                  child: Image.network('https://www.itying.com/images/flutter/1.png', fit: BoxFit.cover,),
                ),
                Text('长沙市')
              ]
            ),
          ),
          Container(
            width: 100,
            margin: EdgeInsets.all(10),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.red,)
            ),
            child: Column(
              children: [
                SizedBox(
                  height: 70,
                  child: Image.network('https://www.itying.com/images/flutter/1.png', fit: BoxFit.cover,),
                ),
                Text('长沙市')
              ]
            ),
          ),
          Container(
            width: 100,
            margin: EdgeInsets.all(10),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.red,)
            ),
            child: Column(
              children: [
                SizedBox(
                  height: 70,
                  child: Image.network('https://www.itying.com/images/flutter/1.png', fit: BoxFit.cover,),
                ),
                Text('长沙市')
              ]
            ),
          ),
          Container(
            width: 100,
            margin: EdgeInsets.all(10),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.red,)
            ),
            child: Column(
              children: [
                SizedBox(
                  height: 70,
                  child: Image.network('https://www.itying.com/images/flutter/1.png', fit: BoxFit.cover,),
                ),
                Text('长沙市')
              ]
            ),
          ),
          Container(
            width: 100,
            margin: EdgeInsets.all(10),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.red,)
            ),
            child: Column(
              children: [
                SizedBox(
                  height: 70,
                  child: Image.network('https://www.itying.com/images/flutter/1.png', fit: BoxFit.cover,),
                ),
                Text('长沙市')
              ]
            ),
          ),
          Container(
            width: 100,
            margin: EdgeInsets.all(10),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.red,)
            ),
            child: Column(
              children: [
                SizedBox(
                  height: 70,
                  child: Image.network('https://www.itying.com/images/flutter/1.png', fit: BoxFit.cover,),
                ),
                Text('长沙市')
              ]
            ),
          ),
          Container(
            width: 100,
            margin: EdgeInsets.all(10),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.red,)
            ),
            child: Column(
              children: [
                SizedBox(
                  height: 70,
                  child: Image.network('https://www.itying.com/images/flutter/1.png', fit: BoxFit.cover,),
                ),
                Text('长沙市')
              ]
            ),
          ),
          Container(
            width: 100,
            margin: EdgeInsets.all(10),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.red,)
            ),
            child: Column(
              children: [
                SizedBox(
                  height: 70,
                  child: Image.network('https://www.itying.com/images/flutter/1.png', fit: BoxFit.cover,),
                ),
                Text('长沙市')
              ]
            ),
          ),
        ],
      )
    );
  }
}

// 动态列表
class List4 extends StatelessWidget {
  const List4({super.key});

  List<Widget> _initListData() {
    List<Widget> list = [];
    for (var i = 0; i < 20; i++) {
      list.add(ListTile(
        title: Text("这是列表---$i"),
      ));
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: _initListData(),
    );
  }
}

class List5 extends StatelessWidget {
  const List5({super.key});

  List<Widget> _initListData() {
    List<Widget> list = [];
    for (var i = 0; i < dataList.length; i++) {
      list.add(ListTile(
        leading: Image.network(dataList[i]['imagePath'].toString()),
        title: Text(dataList[i]['title'].toString()),
        subtitle: Text(dataList[i]['desc'].toString()),
      ));
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      // 方法一
      // children: _initListData(),
      // 方法二
      children: dataList.map((item) {
        return ListTile(
          leading: Image.network(item['imagePath'].toString()),
          title: Text(item['title'].toString()),
          subtitle: Text(item['desc'].toString()),
        );
      }).toList(),
    );
  }
}


class List6 extends StatelessWidget {
  const List6({super.key});

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: 10,
      itemBuilder: (content, index) {
        return ListTile(
          title: Text('这是动态列表----${index+1}'),
        );
      },
    );
  }
}

